<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Question;

use AuctionCore\ClcwUser\ClcwUser;

class QuestionAnswer
{
    private $questionAnswerModel;

    public function __construct()
    {
        $this->questionAnswerModel = M('question_answer', 'clcw_', 'DB_CLCW');
    }

    /**
     * 获取举报回答列表
     *
     * @param int   $page
     * @param int   $size
     * @param array $params
     */
    public function getReportAnswerList($page = 1, $size = 10, $params = [])
    {
        $map = [];
        $map['report_num'] = ['gt', 0];
        $uc = new ClcwUser();
        if ($params['a_id']) {
            $map['answer_id'] = ['eq', $params['a_id']];
        }
        if ($params['a_keywords']) {
            $map['content'] = ['LIKE', '%' . $params['a_keywords'] . '%'];
        }
        if ($params['a_uid']) {
            $map['user_id'] = ['eq', $params['a_uid']];
        }
        if ($params['a_user_name']) {
            $user_id = $uc->getUserIdByNickName($params['a_user_name']);
            $map['user_id'] = ['eq', $user_id];
        }
        if ($params['a_status']) {
            $map['status'] = ['eq', $params['a_status']];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        $cols = 'answer_id,user_id,question_id,content,status,report_num,create_time';
        $order = 'answer_id DESC';
        $total = $this->questionAnswerModel->field($cols)
            ->where($map)->count();
        $list = $this->questionAnswerModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $userInfo = $uc->getClcwUserInfo($v['user_id']);
                $list[$k]['user_name'] = $userInfo['nick_name'];
                $list[$k]['answer_status'] = C('ANSWER_STATUS')[$v['status']];
                $list[$k]['content'] = $v['content'];
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;

    }


    /**
     * 获取嫌疑回答列表
     *
     * @param int   $page
     * @param int   $size
     * @param array $params
     */
    public function get_suspect_answer_list($page = 1, $size = 10, $params = [])
    {
        $map = [];
        $map['status'] = ['eq', 6];
        $uc = new ClcwUser();
        if ($params['a_id']) {
            $map['answer_id'] = ['eq', $params['a_id']];
        }
        if ($params['a_keywords']) {
            $map['content'] = ['LIKE', '%' . $params['a_keywords'] . '%'];
        }
        if ($params['a_uid']) {
            $map['user_id'] = ['eq', $params['a_uid']];
        }
        if ($params['a_user_name']) {
            $user_id = $uc->getUserIdByNickName($params['a_user_name']);
            $map['user_id'] = ['eq', $user_id];
        }
        if ($params['a_status']) {
            $map['status'] = ['eq', $params['a_status']];
        }
        if ($params['start_time']) {
            $map['create_time'] = ['egt', strtotime($params['start_time'] . ' 00:00:00')];
        }
        if ($params['end_time']) {
            $map['create_time'] = ['elt', strtotime($params['end_time'] . ' 23:59:59')];
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_time'] = [
                ['egt', strtotime($params['start_time'] . ' 00:00:00')],
                ['elt', strtotime($params['end_time'] . ' 23:59:59')]
            ];
        }
        $cols = 'answer_id,user_id,question_id,content,status,report_num,create_time';
        $order = 'answer_id DESC';
        $total = $this->questionAnswerModel->field($cols)
            ->where($map)->count();
        $list = $this->questionAnswerModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $userInfo = $uc->getClcwUserInfo($v['user_id']);
                $list[$k]['user_name'] = $userInfo['nick_name'];
                $list[$k]['answer_status'] = C('ANSWER_STATUS')[$v['status']];
                $list[$k]['content'] = $v['content'];
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;

    }


    /**
     * 获取提问对应的答案
     *
     * @param int   $questionId
     * @param array $condition
     */
    public function getQuestionAnswerList($questionId = 0, $condition = [])
    {
        $condition['question_id'] = ['eq', $questionId];
        $cols = 'answer_id,user_id,content,is_accept,is_recommend,status,create_time';
        $list = $this->questionAnswerModel->field($cols)->where($condition)->select();
        if (!empty($list)) {
            $uc = new ClcwUser();
            foreach ($list as $k => $v) {
                $userInfo = $uc->getClcwUserInfo($v['user_id']);
                $list[$k]['user_name'] = empty($userInfo['nick_name']) ? $userInfo['mobile'] : $userInfo['nick_name'];
                $list[$k]['user_avatar'] = getFullUrl($userInfo['avatar']);
                $list[$k]['create_time'] = date("Y-m-d H:i:s", $v['create_time']);
            }
        }
        return $list;
    }


    /**
     * 根据回答ID获得回答详情
     *
     * @param int $answer_id
     */
    public function getQuestionAnswer($answer_id = 0)
    {
        if (empty($answer_id)) {
            return false;
        }
        return $this->questionAnswerModel->find($answer_id);
    }

    /**
     * 更新问题的回答
     *
     * @param array $data
     *
     * @return bool
     */
    public function updateQuestionAnswer($data = [])
    {
        if (empty($data) || empty($data['answer_id'])) {
            return false;
        }
        return $this->questionAnswerModel->save($data);
    }

    /**
     * 更新回答是否编辑推荐
     *
     * @param $questionId
     * @param $answerId
     * @param $isRecommend
     *
     * @return bool
     */
    public function updateAnswerRecommend($questionId = 0, $answerId = 0, $isRecommend = 0)
    {
        if (empty($questionId) || empty($answerId)) {
            return false;
        }
        $map['question_id'] = ['eq', $questionId];
        $map['is_recommend'] = ['eq', 1];
        if ($isRecommend == 1) {
            $total = $this->questionAnswerModel->where($map)->count();
            if ($total > 0) {
                return false;
            }
        }
        M('', 'clcw_', 'DB_CLCW')->startTrans();
        $data['answer_id'] = $answerId;
        $data['is_recommend'] = $isRecommend;
        $ret1 = $this->questionAnswerModel->save($data);
        if ($isRecommend == 1) {
            $ret2 = M('question', 'clcw_', 'DB_CLCW')->save(['question_id' => $questionId, 'is_recommend' => 1]);
        } else {
            $ret2 = M('question', 'clcw_', 'DB_CLCW')->save(['question_id' => $questionId, 'is_recommend' => 0]);
        }
        if (!$ret1 || !$ret2) {
            M('', 'clcw_', 'DB_CLCW')->rollback();
            return false;
        }
        M('', 'clcw_', 'DB_CLCW')->commit();
        return true;
    }

}